package com.workflow.workflowproject.serviceImpl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.workflow.workflowproject.entity.FlowInfo;
import com.workflow.workflowproject.entity.table.FlowInfoTableDef;
import com.workflow.workflowproject.mapper.FlowInfoMapper;
import com.workflow.workflowproject.service.FlowInfoService;
import com.workflow.workflowproject.tools.Result.Result;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class FlowInfoServiceImpl extends ServiceImpl<FlowInfoMapper, FlowInfo> implements FlowInfoService {
    /**
     * 添加流程信息
     *
     * @param
     * @return
     */
    @Override
    public Result addFlowInfo(FlowInfo flowInfo) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(FlowInfoTableDef.FLOW_INFO.NO.eq(flowInfo.getNo()));
        FlowInfo flowInfoOld = this.getMapper().selectOneByQuery(queryWrapper);
        if(ObjectUtil.isNotEmpty(flowInfoOld)){
            return Result.fail("流程信息编号重复");
        }
        if(this.getMapper().insert(flowInfo) == 0){
            return Result.fail("添加流程信息失败");
        }else{
            return Result.ok();
        }
    }
    /**
     * 修改流程信息
     *
     * @param
     * @return
     */
    @Override
    public Result updateFlowInfo(Integer id, FlowInfo flowInfo) throws Exception {
        flowInfo.setId(id);
        if(this.getMapper().update(flowInfo) == 0){
            return Result.fail("修改流程信息失败");
        }else{
            return Result.ok();
        }
    }
    /**
     * 删除流程信息
     *
     * @param
     * @return
     */
    @Override
    public Result deleteFlowInfo(Integer id) throws Exception {
        this.getMapper().deleteById(id);
        return Result.ok();
    }
    /**
     * 查询流程信息
     *
     * @param
     * @return
     */
    @Override
    public Result getFlowInfoById(Integer id) throws Exception {
        FlowInfo flowInfoOne = this.getMapper().selectOneById(id);
        return Result.ok(flowInfoOne);
    }
    /**
     * 查询所有流程信息
     *
     * @param
     * @return
     */
    @Override
    public Result getAllFlowInfo() throws Exception {
        List<FlowInfo> flowInfoAll = this.getMapper().selectAll();
        return Result.ok(flowInfoAll,flowInfoAll.size());
    }
    /**
     * 分页查询流程条件
     *
     * @param
     * @return
     */
    @Override
    public Result getFlowInfoPage(Integer page, Integer pageSize) throws Exception {
        Page<FlowInfo> flowInfoPage = this.getMapper().paginateWithRelations(page,pageSize,new QueryWrapper());
        return Result.ok(flowInfoPage.getRecords(),flowInfoPage.getTotalRow());
    }
}
